package com.tencent.common;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;

import org.json.JSONObject;

public class HttpGetRequest {
	
	public static void main(String[] args) {
		System.out.println(getAccessToken());
	}
	

    /**
     * getAccessToken
     * @throws Exception 
     */
    public static String getAccessToken() {
    	try {
	    	String result = doGet();
	    	JSONObject jsonObject = new JSONObject(result);
	    	String data =  jsonObject.get("data").toString();
	    	JSONObject jsonData = new JSONObject(data);
	    	String accessToken = jsonData.get("accessToken").toString();
	    	return accessToken;
    	} catch (Exception e) {
			return "";
		}
    }
    
    /**
     * getJsapiTicket
     * @throws Exception 
     */
    public static String getJsapiTicket() {
    	try {
        	String result = doGet();
        	JSONObject jsonObject = new JSONObject(result);
        	String data =  jsonObject.get("data").toString();
        	JSONObject jsonData = new JSONObject(data);
        	String jsapiTicket = jsonData.get("jsapiTicket").toString();
        	return jsapiTicket;
		} catch (Exception e) {
			return "";
		}
    }
    
    /**
     * Get Request
     * @return
     * @throws Exception
     */
    private static String doGet() throws Exception {
        URL localURL = new URL("http://wx.tongguanedu.com/web/wxAuth/getAccessToken");
        URLConnection connection = localURL.openConnection();
        HttpURLConnection httpURLConnection = (HttpURLConnection)connection;
        
        httpURLConnection.setRequestProperty("Accept-Charset", "utf-8");
        httpURLConnection.setRequestProperty("Content-Type", "application/json;charset=utf-8");
        httpURLConnection.setRequestProperty("crossDomain", "true");
        
        InputStream inputStream = null;
        InputStreamReader inputStreamReader = null;
        BufferedReader reader = null;
        StringBuffer resultBuffer = new StringBuffer();
        String tempLine = null;
        
        if (httpURLConnection.getResponseCode() >= 300) {
            throw new Exception("HTTP Request is not success, Response code is " + httpURLConnection.getResponseCode());
        }
        
        try {
            inputStream = httpURLConnection.getInputStream();
            inputStreamReader = new InputStreamReader(inputStream);
            reader = new BufferedReader(inputStreamReader);
            
            while ((tempLine = reader.readLine()) != null) {
                resultBuffer.append(tempLine);
            }
            
        } finally {
            
            if (reader != null) {
                reader.close();
            }
            
            if (inputStreamReader != null) {
                inputStreamReader.close();
            }
            
            if (inputStream != null) {
                inputStream.close();
            }
            
        }
        
        return resultBuffer.toString();
    }
    
    
}

